package com.aurora.calculator.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.aurora.calculator.CalculatorViewModel
import com.aurora.calculator.data.symbols
import com.aurora.calculator.state.CalculatorEvents
import com.aurora.calculator.state.CalculatorOperations
import com.aurora.calculator.state.CalculatorState
import com.aurora.calculator.ui.theme.CalculatorTheme
import kotlin.text.contains

/**
 * 计算器界面
 */
@Composable
fun CalculatorScreen(
    modifier: Modifier = Modifier,
    state: CalculatorState,
    onEvents: (CalculatorEvents) -> Unit
) {
    Box(modifier = modifier) {
        Column(
            modifier = Modifier.fillMaxSize(),
            verticalArrangement = Arrangement.spacedBy(15.dp)
        ) {
            Text(
                modifier = Modifier.fillMaxWidth(),
                text = "Calc",
                fontWeight = FontWeight.Bold,
                fontSize = 40.sp,
                color = CalculatorTheme.colors.ResultColor
            )
            ResultTextComponent(state)
            Spacer(modifier = Modifier.fillMaxWidth().height(10.dp))
            KeyBoardComponent(state, onEvents)
        }
    }
}

/**
 * 输入结果显示框
 */
@Composable
private fun ColumnScope.ResultTextComponent(state: CalculatorState) {
    Column(
        modifier = Modifier.fillMaxWidth().weight(1f),
        horizontalAlignment = Alignment.End,
        verticalArrangement = Arrangement.Bottom
    ) {
        Text(
            modifier = Modifier
                .fillMaxWidth(),
            text = if (state.leftNumber.isNotBlank())
                state.leftNumber + (state.operation?.symbol ?: "") + state.rightNumber
            else state.result,
            textAlign = TextAlign.End,
            fontSize = 65.sp,
            lineHeight = 65.sp,
            softWrap = true,
            maxLines = 2,
            color = CalculatorTheme.colors.ResultColor
        )

    }
}

/**
 * 键盘
 */
@Composable
private fun KeyBoardComponent(state: CalculatorState, onEvents: (CalculatorEvents) -> Unit) {
    // 键盘内容
    val buttonSymbol = arrayListOf(
        arrayOf(
            symbols[0][0] to CalculatorTheme.colors.AcColor,
            symbols[0][1] to CalculatorTheme.colors.SymbolColor,
            symbols[0][2] to CalculatorTheme.colors.SymbolColor,
            symbols[0][3] to CalculatorTheme.colors.SymbolColor
        ),
        arrayOf(
            symbols[1][0] to CalculatorTheme.colors.ButtonColor,
            symbols[1][1] to CalculatorTheme.colors.ButtonColor,
            symbols[1][2] to CalculatorTheme.colors.ButtonColor,
            symbols[1][3] to CalculatorTheme.colors.SymbolColor
        ),
        arrayOf(
            symbols[2][0] to CalculatorTheme.colors.ButtonColor,
            symbols[2][1] to CalculatorTheme.colors.ButtonColor,
            symbols[2][2] to CalculatorTheme.colors.ButtonColor,
            symbols[2][3] to CalculatorTheme.colors.SymbolColor
        ),
        arrayOf(
            symbols[3][0] to CalculatorTheme.colors.ButtonColor,
            symbols[3][1] to CalculatorTheme.colors.ButtonColor,
            symbols[3][2] to CalculatorTheme.colors.ButtonColor,
            symbols[3][3] to CalculatorTheme.colors.SymbolColor
        ),
        arrayOf(
            symbols[4][0] to CalculatorTheme.colors.ButtonColor,
            symbols[4][1] to CalculatorTheme.colors.ButtonColor,
            symbols[4][2] to CalculatorTheme.colors.ButtonColor,
            symbols[4][3] to CalculatorTheme.colors.EqualColor
        )
    )

    buttonSymbol.forEach {
        Row(horizontalArrangement = Arrangement.spacedBy(15.dp)) {
            it.forEach {
                CalculatorButton(
                    modifier = Modifier
                        .weight(1f)
                        .aspectRatio(1f)
                        .background(it.second),
                    symbol = it.first
                ) {
                    when (it.first) {
                        in "0" -> onEvents(CalculatorEvents.Number("0"))
                        in "00" -> onEvents(CalculatorEvents.Number(if (state.leftNumber != "" && state.leftNumber != "0") "00" else "0"))
                        in "1" -> onEvents(CalculatorEvents.Number("1"))
                        in "2" -> onEvents(CalculatorEvents.Number("2"))
                        in "3" -> onEvents(CalculatorEvents.Number("3"))
                        in "4" -> onEvents(CalculatorEvents.Number("4"))
                        in "5" -> onEvents(CalculatorEvents.Number("5"))
                        in "6" -> onEvents(CalculatorEvents.Number("6"))
                        in "7" -> onEvents(CalculatorEvents.Number("7"))
                        in "8" -> onEvents(CalculatorEvents.Number("8"))
                        in "9" -> onEvents(CalculatorEvents.Number("9"))
                        in "AC" -> onEvents(CalculatorEvents.Clear)
                        in "+" -> onEvents(CalculatorEvents.Operation(CalculatorOperations.Add))
                        in "-" -> onEvents(CalculatorEvents.Operation(CalculatorOperations.Subtract))
                        in "×" -> onEvents(CalculatorEvents.Operation(CalculatorOperations.Multiply))
                        in "÷" -> onEvents(CalculatorEvents.Operation(CalculatorOperations.Divide))
                        in "%" -> onEvents(CalculatorEvents.Operation(CalculatorOperations.Mod))
                        in "." -> onEvents(CalculatorEvents.Decimal)
                        in "=" -> onEvents(CalculatorEvents.Calculator)
                        in "DE" -> onEvents(CalculatorEvents.Delete)
                        else -> {}
                    }
                }
            }
        }
    }
}


@Composable
@Preview
fun ShowScreen() {
    CalculatorTheme {
        val viewModel = viewModel<CalculatorViewModel>()
        val state = viewModel.state

        CalculatorScreen(
            state = state,
            modifier = Modifier
                .fillMaxSize()
                .background(CalculatorTheme.colors.BackgroundColor)
                .padding(16.dp),
            onEvents = viewModel::onEvents
        )
    }
}
